package com.xdf.whiteaccount.service.impl;

import java.util.Optional;
import java.util.List;

import com.xdf.whiteaccount.service.DefectivegoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Lazy;
import com.xdf.whiteaccount.dao.DefectivegoodsMapper;
import com.xdf.whiteaccount.entity.Defectivegoods;
import com.xdf.whiteaccount.utils.BusinessException;
import com.xdf.whiteaccount.utils.Example;

/**
 * 版本号：20240401
 * @Description : 服务层实现类，自定义代码写在最后，不要插在中间
 * @Author : chanchaw
 * @Date : 2025-9-5 14:54:28
 */
@Service
public class DefectivegoodsServiceImpl implements DefectivegoodsService {
    @Autowired
    private DefectivegoodsMapper dao;

    @Lazy
    @Autowired
    private DefectivegoodsService meService;

    @Override
    @Transactional
    public Defectivegoods insert(Defectivegoods record) {
        meService.check4Insert(record);
        record = meService.fullFill(record);
        meService.doBeforeInsert(record);
        dao.insert(record);
        meService.doAfterInsert(record);
        return record;
    }

    @Override
    @Transactional
    public Defectivegoods insertSelective(Defectivegoods record) {
        meService.check4Insert(record);
        record = meService.fullFill(record);
        doBeforeInsert(record);
        dao.insertSelective(record);
        meService.doAfterInsert(record);
        return record;
    }

    /* 由于本方法无法返回所有对象的自增主键，后者调整为一个一个保存的方法
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<Defectivegoods> list) {
        return dao.multiInsert(list);
    }
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Defectivegoods> multiInsert(List<Defectivegoods> list) {
        list.forEach(item -> meService.insertSelective(item));
        return list;
    }

    @Override
    public int updateByPrimaryKey(Defectivegoods record) {
        checkSid4Update(record);
        return dao.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(Defectivegoods record) {
        checkSid4Update(record);
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer B_itemId) {
        return dao.deleteByPrimaryKey(B_itemId);
    }

    @Override
    public Defectivegoods selectByPrimaryKey(Integer B_itemId) {
        return dao.selectByPrimaryKey(B_itemId);
    }

    @Override
    public List<Defectivegoods> selectAll(){
        return dao.selectAll();
    }

    @Override
    public Defectivegoods save(Defectivegoods record){
        if(record == null) throw new BusinessException("空对象不可保存！");
        Integer iid = Optional.ofNullable(record.getBitemId()).orElse(0);
        if(iid.equals(0)) record = insertSelective(record);
        else updateByPrimaryKeySelective(record);
        return record;
    }

    // 没有使用 xml 中的 multiSave 方法是因为无法批量回写自增主键
    @Transactional
    @Override
    public List<Defectivegoods> multiSave(List<Defectivegoods> list){
        if(list == null || list.size()==0) return list;
        list.forEach(item -> item = meService.save(item));
        return list;
    }

    /**=========================下面开始是固定模式代码=======================================*/
    public Defectivegoods selectBySid(String sid){
        return dao.selectBySid(sid);
    }

    // 返回true表示存在，false表示不存在
    // 基础资料都以字段 sid 为关键标识，自增主键 iid 备用
    public Boolean exists(String sid){
        Defectivegoods record = selectBySid(sid);
        return record == null ? false : true;
    }

    // 新增前调用本方法将对象填充完整，然后调用 doBeforeInsert 再进行插入
    @Override
    public Defectivegoods fullFill(Defectivegoods record) {
        // TODO: 默认前端传递来的参数不需要填充完整，如果需要则使用自定义逻辑覆盖下面的代码
        return DefectivegoodsService.super.fullFill(record);
    }

    // 新增前调用的方法，抛出异常则不会新增数据
    @Override
    public void doBeforeInsert(Defectivegoods record) {
        DefectivegoodsService.super.doBeforeInsert(record);
    }

    // 新增后调用的方法
    @Override
    public void doAfterInsert(Defectivegoods record) {
        DefectivegoodsService.super.doAfterInsert(record);
    }

    // 是基础资料数据则修改之前要调用本方法
    // 检测修改后的目标 sid 是否已经存在
    // 这里要防止检测到自己 - 根据主键 iid 判断
    private void checkSid4Update(Defectivegoods record){
//        if(record == null) throw new BusinessException("传入了空对象，无法修改！");
//        Integer iid = Optional.ofNullable(record.getBItemId()).orElse(0);
//        if(iid.equals(0)) throw new BusinessException("没有主键无法修改！");
//        Defectivegoods selectedRecord = selectByPrimaryKey(iid);
//        if(selectedRecord == null) throw new BusinessException("主键" +iid+"的数据没有找到！");
//        String sid = Optional.ofNullable(record.getBItemId()).orElse("");
//        Example<Defectivegoods> example = new Example<>();
//        example.andIn("sid",sid);
//        List<Defectivegoods> records = dao.selectByExample(example);
//        if(records.size() == 0) return;
//        records.forEach(item -> {
//            if(!item.getIid().equals(iid)) throw new BusinessException("编号" +sid+"已经存在，请更换后再次保存！");
//        });
    }

    @Override
    public List<Defectivegoods> selectByExample(Example<Defectivegoods> example){
        return dao.selectByExample(example);
    }
    //=================以上是自动生成的代码，自定义代码请写在下面==================
    @Override
    public List<Defectivegoods> selectByParam(Defectivegoods record){
        return dao.selectByParam(record);
    }
}